#include "func.h"
#include <stdio.h>
#include <string.h>
#include <string>
#include <sys/types.h>
#include <sys/stat.h> 
#include <iostream>
#include <stdlib.h>
using namespace std;

ParseDB::ParseDB(const char* from_db_name)
{
	cnt = 1;
	m_from_dbname = from_db_name;

	basestr = m_from_dbname;
	basestr = basestr.substr(2,strlen(m_from_dbname)-4-2);
	mkdir(basestr.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);

	pFromFile = fopen(m_from_dbname,"rb");
	if(pFromFile == NULL)
	{
		printf("fopen from file error %s\n",m_from_dbname);
	}
	
	infoFile = basestr + "/" + basestr + ".info";
	pInfoFile = fopen(infoFile.c_str(),"wb");
	if(pInfoFile == NULL)
	{
		printf("create info file error=%s\n",infoFile.c_str());
	}

	string errFile = basestr + "/" + basestr + ".err";
	pErrorFile = fopen(errFile.c_str(),"ab+");
	if(pErrorFile == NULL)
	{
		printf("open error file error=%s\n",errFile.c_str());
	}
}
void ParseDB::error_output(char* err_str)
{
	fprintf(pErrorFile,"%s\n",err_str);
}

void ParseDB::info_output(Info_t info)
{
	fprintf(pInfoFile,"%s %-30s %-20s %s %s %s %s\n",
			info.from_db_name,
			info.uname,
			info.passwd,
			info.u_md5,
			info.p_md5,
			info.u_sha1,
			info.p_sha1);
	cnt++;
	if(cnt%100 == 0)
	{
		fclose(pInfoFile);
		char a[100] = {'\0'};
		sprintf(a,"%d",cnt/100);
		string renameFile = basestr + "/" + basestr + a +".info";
		rename(infoFile.c_str(),renameFile.c_str());
		pInfoFile = fopen(infoFile.c_str(),"wb");
		if(pInfoFile == NULL)
		{
			printf("rereate info file error=%s\n",infoFile.c_str());
		}
	}
}
void ParseDB::start_parse()
{
	fclose(pFromFile);
	fclose(pErrorFile);
	fclose(pInfoFile);
}


//12306
void Parse12306DB::start_parse()
{
	printf("parse 12306 db start_parse\n");
	char tmp[1024];
	char* p;
	int ret;
	Info_t info;
	const char* divider = "----";
	while( (p=fgets(tmp, sizeof(tmp), pFromFile)) !=NULL )
	{
		if( *p == '\n' )
		{
			continue;
		}
		
		char mail[50];
		char account[50];
		char name[50];
		char id[19];
		char pass[50];
		char phone[12];

		for(char*f=p; *f!='\0';f++  )
		{
			if(*f == '-')
				if(*(f+1)=='-')
					if(*(f+2)=='-')
						if(*(f+3)=='-')
						{
							*f = '\t';
							*(f+1) = '\t';
							*(f+2) = '\t';
							*(f+3) = '\t';
							f += 3;
						}

		}

		ret = sscanf(p,"%[^\t ]%*[\t ]%[^\t ]%*[\t ]%[^\t ]%*[\t ]%[^\t ]%*[\t ]%[^\t ]%*[\t ]%[^\t ]",
				mail,  account,name,id, pass,  phone);

		if(ret != 6) 
		{
			printf("error:%s\n",p);
			error_output(p);
		}
		//mail as account 
		info.from_db_name = "12306";
		info.uname = mail;
		info.passwd = pass;
		info.u_md5 = "u_md5";
		info.p_md5 = "p_md5";
		info.u_sha1 = "u_sha1";
		info.p_sha1 = "p_sha1";
		info_output(info);
		//account as account
		info.uname = account;
		info_output(info);
		//phone as account
		info.uname = phone;
		info_output(info);
	}

	fclose(pFromFile);
	fclose(pErrorFile);
	fclose(pInfoFile);
}

//7k7k
void Parse7k7kDB::start_parse()
{
	printf("parse 7k7k  db start_parse\n");
	char tmp[1024];
	char* p;
	int ret;
	Info_t info;
	while( (p=fgets(tmp, sizeof(tmp), pFromFile)) !=NULL )
	{
		if( *p == '\n' )
		{
			continue;
		}
		char mail[50];
		char pass[50];
		ret = sscanf(p,"%[^\t ]%*[\t ]%[^\t \n]",mail,pass);
		if(ret != 2)
		{
			printf("error:%s\n",p);
			error_output(p);
			continue;
		}
		//mail as account 
		info.from_db_name = "7k7k";
		info.uname = mail;
		info.passwd = pass;
		info.u_md5 = "u_md5";
		info.p_md5 = "p_md5";
		info.u_sha1 = "u_sha1";
		info.p_sha1 = "p_sha1";
		info_output(info);

	}

	fclose(pFromFile);
	fclose(pErrorFile);
	fclose(pInfoFile);
}

//duduniu
void ParseDuduniuDB::start_parse()
{
	printf("parse duduniu db start_parse\n");
	char tmp[1024];
	char* p;
	int ret;
	Info_t info;
	while( (p=fgets(tmp, sizeof(tmp), pFromFile)) !=NULL )
	{
		if( *p == '\n' )
		{
			continue;
		}
		char mail[50];
		char pass[50];
		char uname[50];
		ret = sscanf(p,"%[^\t ]%*[\t ]%[^\t ]%*[\t ]%[^\t \n]",
			uname,mail,pass);
		if(ret != 3)
		{
			printf("error:%s\n",p);
			error_output(p);
			continue;
		}
		//mail as account 
		info.from_db_name = "duduniu";
		info.uname = mail;
		info.passwd = pass;
		info.u_md5 = "u_md5";
		info.p_md5 = "p_md5";
		info.u_sha1 = "u_sha1";
		info.p_sha1 = "p_sha1";
		info_output(info);
		//account as account
		info.uname = uname;
		info_output(info);
	}

	fclose(pFromFile);
	fclose(pErrorFile);
	fclose(pInfoFile);
}

//shengda
void ParseShengdaDB::start_parse()
{
	printf("parse shengda db start_parse\n");
	char tmp[1024];
	char* p;
	int ret;
	Info_t info;
	while( (p=fgets(tmp, sizeof(tmp), pFromFile)) !=NULL )
	{
		if( *p == '\n' )
		{
			continue;
		}
		for(char*f=p; *f!='\0';f++  )
		{
			if(*f == '-')
				if(*(f+1)=='-')
					if(*(f+2)=='-')
						if(*(f+3)=='-')
						{
							*f = '\t';
							*(f+1) = '\t';
							*(f+2) = '\t';
							*(f+3) = '\t';
							f += 3;
						}

		}

		char pass[50];
		char uname[50];
		ret = sscanf(p,"%[^\t]%*[\t]%[^\t \n]",
			uname,pass);
		if(ret != 2)
		{
			printf("error:%s\n",p);
			error_output(p);
			continue;
		}
		//account as account
		info.from_db_name = "shengda";
		info.uname = uname;
		info.passwd = pass;
		info.u_md5 = "u_md5";
		info.p_md5 = "p_md5";
		info.u_sha1 = "u_sha1";
		info.p_sha1 = "p_sha1";
		info_output(info);
	}

	fclose(pFromFile);
	fclose(pErrorFile);
	fclose(pInfoFile);
}

void ParseWangyiDB::start_parse()
{
	printf("parse wangyi db start_parse\n");
	char tmp[1024];
	char* p;
	int ret;
	Info_t info;
	while( (p=fgets(tmp, sizeof(tmp), pFromFile)) !=NULL )
	{
		if( *p == '\n' )
		{
			continue;
		}

		for(char*f=p; *f!='\0';f++  )
		{
			if(*f == '-')
				if(*(f+1)=='-')
					if(*(f+2)=='-')
						if(*(f+3)=='-')
						{
							*f = '\t';
							*(f+1) = '\t';
							*(f+2) = '\t';
							*(f+3) = '\t';
							f += 3;
						}

		}


		char pass[50];
		char mail[50];
		ret = sscanf(p,"%[^ \t]%*[ \t]%[^\t \n]",
			mail,pass);
		if(ret != 2)
		{
			printf("error:%s\n",p);
			error_output(p);
			continue;
		}
		//mail as account
		info.from_db_name = "wangyi";
		info.uname = mail;
		info.passwd = pass;
		info.u_md5 = "u_md5";
		info.p_md5 = "p_md5";
		info.u_sha1 = "u_sha1";
		info.p_sha1 = "p_sha1";
		info_output(info);
	}

	fclose(pFromFile);
	fclose(pErrorFile);
	fclose(pInfoFile);
}

void ParseCsdnDB::start_parse()
{
	printf("parse csdn db start_parse\n");
	char tmp[1024];
	char* p;
	int ret;
	Info_t info;
	while( (p=fgets(tmp, sizeof(tmp), pFromFile)) !=NULL )
	{
		if( *p == '\n' )
		{
			continue;
		}
		char pass[50];
		char account[50];
		char mail[50];
		ret = sscanf(p,"%[^# ] # %[^# ] # %[^ \n]",
			account,pass,mail);
		if(ret != 3)
		{
			printf("error:%s\n",p);
			error_output(p);
			continue;
		}
		//account as account
		info.from_db_name = "csdn";
		info.uname = account;
		info.passwd = pass;
		info.u_md5 = "u_md5";
		info.p_md5 = "p_md5";
		info.u_sha1 = "u_sha1";
		info.p_sha1 = "p_sha1";
		info_output(info);
		//mail as account
		info.uname = mail;
		info_output(info);
	}

	fclose(pFromFile);
	fclose(pErrorFile);
	fclose(pInfoFile);
}
